$(function () {
    $("#jqGrid").jqGrid({
        url: '../ywreward/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true ,hidden:true},
			{ label: '员工编号', name: 'employeeid', index: 'employeeid', width: 80 ,hidden:true},
			{ label: '员工姓名', name: 'employeename', index: 'employeename', width: 80 }, 			
			{ label: '员工身份证', name: 'employeecard', index: 'employeecard', width: 80 }, 			
			{ label: '单位名称', name: 'companyname', index: 'companyname', width: 80 },
			{ label: '单位编码', name: 'companyid', index: 'companyid', width: 80,hidden:true },
			{ label: '部门', name: 'department', index: 'department', width: 80 }, 			
			{ label: '岗位', name: 'station', index: 'station', width: 80 }, 			
			{ label: '项目组', name: 'project', index: 'project', width: 80 }, 			
			{ label: '类别', name: 'rewardtype', index: 'rewardtype', width: 80 ,formatter:getDictReward},
            { label: '原因', name: 'reason', index: 'reason', width: 80 },
			{ label: '结果（金额）', name: 'result', index: 'result', width: 80 }, 			
			{ label: '奖惩日期', name: 'occurtime', index: 'occurtime', width: 80 },
            { label: '执行状态', name: 'status', index: 'status', width: 80 ,formatter:getstatus,unformat:ungetstatus},
            { label: '记录人', name: 'writer', index: 'writer', width: 80 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        //字段太多自动加载横轴滚动条
        shrinkToFit:false,
        autoScroll: false,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	//$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
    vm.getjqGridpersondispatch();
    var r = utils.getDictList("rewards");
    vm.rewards=r.rewards;
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q: {
            employeename: null,
            companyname: null,
            rewardtype: null
        },
		showList: true,
		title: null,
		ywReward: {},
        rewards:{}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
            vm.ywReward= {
                employeename:"",
                occurtime:null
            };
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id);
		},
		saveOrUpdate: function (event) {
			var url = vm.ywReward.id == null ? "../ywreward/save" : "../ywreward/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.ywReward),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../ywreward/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../ywreward/info/"+id, function(r){
                vm.ywReward = r.ywReward;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    employeename: vm.q.employeename,
                    companyname: vm.q.companyname,
                    rewardtype: vm.q.rewardtype
                },
                page:page
            }).trigger("reloadGrid");
		},
		getjqGridpersondispatch: function(){
            $("#jqGridpersondispatch").jqGrid({
                url: '../persondispatch/list',
                datatype: "json",
                colModel: [
                    { label: 'id', name: 'id', index: 'id', key: true, hidden: true },
                    { label: '员工名称', name: 'name', index: 'name' },
                    { label: '身份证', name: 'idno', index: 'idno' },
                    { label: '公司名称', name: 'settlementName', index: 'settlement_name' },
                    { label: '结算点id', name: 'settlementId', index: 'settlement_id' ,hidden:true},
                    { label: '人员id', name: 'personId', index: 'person_id' ,hidden:true},
                    { label: '入职日期', name: 'entryDate', index: 'entry_date' },
                    { label: '部门', name: 'department', index: 'department' },
                    { label: '岗位', name: 'station', index: 'station' },
                    { label: '派遣类型', name: 'dispatchType', index: 'dispatch_type' },
                    { label: '合同id', name: 'contractId', index: 'contract_id' ,hidden:true},
                    { label: '档案状态:未接收、待接收', name: 'fileState', index: 'file_state',width: 300},
                    { label: '档案id', name: 'archivesId', index: 'archives_id',hidden:true },
                    { label: '工伤:未申报、预申报', name: 'injury', index: 'injury' ,width: 300},
                    { label: '工伤参保日期', name: 'injuryInsuredDate', index: 'injury_insured_date'  },
                    { label: '工伤退保日期', name: 'injurySurrenderDate', index: 'injury_surrender_date' },
                    { label: '养老:未申报、预申报', name: 'pension', index: 'pension' ,width: 300 },
                    { label: '养老参保日期', name: 'pensionInsuredDate', index: 'pension_insured_date' },
                    { label: '养老退保日期', name: 'pensionSurrenderDate', index: 'pension_surrender_date'  },
                    { label: '失业参保情况', name: 'unemployment', index: 'unemployment'},
                    { label: '失业参保日期', name: 'unemploymentInsuredDate', index: 'unemployment_insured_date' },
                    { label: '失业退保日期', name: 'unemploymentSurrenderDate', index: 'unemployment_surrender_date' },
                    { label: '医保', name: 'medicare', index: 'medicare' },
                    { label: '医保申报日期', name: 'medicareInsuredDate', index: 'medicare_insured_date' },
                    { label: '医保退保日期', name: 'medicareSurrenderDate', index: 'medicare_surrender_date' },
                    { label: '生育', name: 'maternity', index: 'maternity'  },
                    { label: '生育参保日期', name: 'maternityInsuredDate', index: 'maternity_insured_date' },
                    { label: '生育退保日期', name: 'maternitySurrenderDate', index: 'maternity_surrender_date'  },
                    { label: '公积金', name: 'accumulationFund', index: 'accumulation_fund'  },
                    { label: '公积金参保日期', name: 'accumulationFundInsuredDate', index: 'accumulation_fund_insured_date' },
                    { label: '公积金退保日期', name: 'accumulationFundSurrenderDate', index: 'accumulation_fund_surrender_date'  },
                    { label: '五险合一:未申报、预申报', name: 'fiveOneInsurance', index: 'five_one_insurance',width: 300 },
                    { label: '操作人', name: 'operator', index: 'operator' },
                    { label: '操作日期', name: 'operationDate', index: 'operation_date'  },
                    { label: '不申报原因', name: 'noDeclareReason', index: 'no_declare_reason'  },
                    { label: '备注', name: 'remark', index: 'remark' },
                    { label: '是否有效', name: 'enable', index: 'enable' }
                ],
                viewrecords: true,
                height: 300,
                width: 900,
                rowNum: 30,
                rowList : [10,30,50],
                rownumbers: true,
                autowidth:false,
                //字段太多自动加载横轴滚动条
                shrinkToFit:false,
                autoScroll: false,
                multiselect:false,//只允许单选
                pager: "#jqGridPagerpersondispatch",
                jsonReader : {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                },
                gridComplete:function(){
                    //doResize();
                    //显示grid底部滚动条
                    //$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "scroll" });
                }
            });
		},
        popemployeename:function(){
            utils.openDialogView('单位员工管理',$("#window"),'900px','500px',1);
		}
	}
});
$("#closepersondispatch").click(function(){
    utils.closeCurrentPage();
});
$("#addpersondispatch").click(function(){
    //判断是否选中了数据
    var id=$("#jqGridpersondispatch").jqGrid('getGridParam','selrow');
    if(id==null){
        alert("请选择一条记录!");
        return ;
    }
    var rowData = $("#jqGridpersondispatch").jqGrid('getRowData',id);
    vm.ywReward.employeeid=rowData.personId;
    vm.ywReward.employeename=rowData.name;
    vm.ywReward.employeecard=rowData.idno;
    vm.ywReward.companyid=rowData.settlementId;
    vm.ywReward.companyname=rowData.settlementName;
    vm.ywReward.department=rowData.department;
    vm.ywReward.station=rowData.station;
    //alert(vm.ywReward.employeeid);
    utils.closeCurrentPage();
});
function getDictReward(cellvalue, options, rowObject){
    var html="";
    for(var i=0; i<vm.rewards.length; i++){
        if(vm.rewards[i].value==cellvalue){
            html=vm.rewards[i].label;
        }
        if(html==undefined){
            html="";
        }
    }
  return html;
}

function setResult(value){
    vm.ywReward.result=value;
}
function getstatus(cellvalue, options, rowObject){
    var html="";
    if(cellvalue=="0"){
        html="未执行";
    }else if(cellvalue=="1"){
        html="已执行";
    }
    return html;
}
function ungetstatus(cellvalue, options, rowObject){
    var html="";
    if(cellvalue=="未执行"){
        html="0";
    }else if(cellvalue=="已执行"){
        html="1";
    }
    return html;
}